package com.chorye.mongo.controller;

import com.chorye.mongo.entity.Student;
import com.chorye.mongo.service.StudentService;
import com.mongodb.client.result.DeleteResult;
import com.mongodb.client.result.UpdateResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * @Company: 北京XXXX有限公司
 * @Author JiangYy
 * @Title: StudentController
 * @Description: TODO
 * @Date 2018/12/24 14:53
 */
@RestController
@RequestMapping("/student")
public class StudentController {

    @Autowired
    private StudentService studentService;

    /**
     * 保存学生
     *
     * @return 保存的学生
     */
    @RequestMapping("/save")
    public Student save() {
        Student student = new Student();
        student.setId(1);
        student.setName("mongo");
        student.setSalary(BigDecimal.valueOf(1000));
        student.setBirth(new Date());
        return studentService.save(student);
    }

    /**
     * 修改学生
     *
     * @return 修改结果
     */
    @RequestMapping("/update")
    public UpdateResult update() {
        Student student = new Student();
        student.setId(1);
        student.setName("mongo");
        student.setSalary(BigDecimal.valueOf(2000));
        student.setBirth(new Date());
        return studentService.update(student);
    }

    /**
     * 根据姓名删除学生
     *
     * @param name 姓名
     * @return 删除结果
     */
    @RequestMapping("/delete")
    public DeleteResult delete(String name) {
        return studentService.delete(name);
    }

    /**
     * 通过名字查找学生
     *
     * @param name 学生名
     * @return 学生信息
     */
    @RequestMapping("/find")
    public Student find(String name) {
        return studentService.find(name);
    }

    /**
     * 查找所有学生
     *
     * @return 学生集合
     */
    @RequestMapping("/all")
    public List<Student> findAll() {
        return studentService.findAll();
    }

}
